package com.xiu.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xiu.pojo.User;

/**
 * Thymeleaf 入门案例
 *
 *
 */
@Controller
public class DemoController {
	@RequestMapping("/show")
	public String showInfo(Model model) {
		model.addAttribute("msg", "Thymeleaf 第一个案例");
		model.addAttribute("key", new Date());
		model.addAttribute("sex", "男");
		model.addAttribute("id", 2);
		List<User> users = new ArrayList<User>();
		users.add(new User(12, "张三", 12));
		users.add(new User(13, "李四", 13));
		users.add(new User(14, "王五", 14));
		model.addAttribute("list", users);
		Map<String, User> map = new HashMap<>();
		map.put("u1", new User(1, "张三", 20));
		map.put("u2", new User(2, "李四", 22));
		map.put("u3", new User(3, "王五", 24));
		model.addAttribute("map", map);
		return "index";
	}

	@RequestMapping("/show2")
	public String showInfo2(HttpServletRequest request) {
		request.setAttribute("req", "HttpServletRequest");
		request.getSession().setAttribute("sess", "HttpSession");
		request.getSession().getServletContext().setAttribute("app", "Application");
		return "index2";
	}

	@RequestMapping("/{page}")
	public String showInfo3(@PathVariable String page, Integer id, String name) {
		System.out.println("id=" + id);
		System.out.println("name=" + name);
		return page;
	}

	@RequestMapping("/path/{page}/show")
	public String showInfo4(@PathVariable String page, Integer id, String name) {
		System.out.println("id=" + id);
		System.out.println("name=" + name);
		System.out.println("page=" + page);
		return page;
	}
}